package ro.adventist.devotin2013;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper  extends SQLiteOpenHelper{
	
	private static String DB_PATH = "/data/data/ro.adventist.devotin2013/databases/";
	private static String DB_NAME = "DevoTin2013.db";
	private SQLiteDatabase myDataBase; 
	private final Context myContext;
	private final static int DATABASE_VERSION = 1;

	public DataBaseHelper(Context context) {
		super(context, DB_NAME, null, 1);
		this.myContext = context;
	}
	
	public void createDataBase() throws IOException{
		boolean dbExist = checkDataBase();
		SQLiteDatabase db_Read = null;
		if(dbExist){
			String myPath = DB_PATH + DB_NAME;
			db_Read = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
			int versionOfActiveDatabase = db_Read.getVersion();
			db_Read.close();
			if (DATABASE_VERSION > versionOfActiveDatabase) {
				myContext.deleteDatabase(DB_NAME);
				SQLiteDatabase db_Read1 = this.getReadableDatabase();
				db_Read1.close();
				try{
					copyDataBase();
				}catch (IOException e) {
				throw new Error("Error In COPYING OF DB");
			}
		}

		}else{
			db_Read = this.getReadableDatabase(); 
			db_Read.close();
			try {
    			copyDataBase();
    		} catch (IOException e) {
        		throw new Error("Error copying database");
        	}
		}
	}

	private boolean checkDataBase(){
		SQLiteDatabase checkDB = null;
		String myPath = DB_PATH + DB_NAME;
		try {
			checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
			
		} catch (SQLiteException e) {
			e.printStackTrace();
		}
		if(checkDB != null){
//			System.out.println("DB Version: "+checkDB.getVersion());
			checkDB.close();
		}
		return checkDB != null ? true : false;
		
	}	
	
	private void copyDataBase() throws IOException{
		InputStream myInput = myContext.getAssets().open(DB_NAME);
		String outFileName = DB_PATH + DB_NAME;
		OutputStream myOutput = new FileOutputStream(outFileName);
		byte[] buffer = new byte[1024];
    	int length;
    	while ((length = myInput.read(buffer))>0){
    		myOutput.write(buffer, 0, length);
    	}
 
    	//Close the streams
    	myOutput.flush();
    	myOutput.close();
    	myInput.close();
	}


    public SQLiteDatabase openDataBase() throws SQLException{
    	 
    	//Open the database
        String myPath = DB_PATH + DB_NAME;
    	return myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
 
    }
	
	   @Override
		public synchronized void close() {
	 
	    	    if(myDataBase != null)
	    		    myDataBase.close();
	 
	    	    super.close();
	 
		}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}

}
